/*
 * Event.cpp
 *
 *  Created on: 2014年5月28日
 *      Author: ellzu
 */

#include <common/EventRunLoop/Event.h>
#include <typeinfo>
#include "common/KGLog.h"
#include "EventContext.h"

namespace EventRunLoop {

Event::Event(int type_,int subType_,bool isAsync)
{
	type = type_;
	subType = subType_;
	userContent = 0;

	_isAsync = isAsync;

	if(_isAsync==false){
		pthread_mutex_init(&_mutex,0);
		pthread_cond_init(&_cond,0);
	}else{
//		_mutex = 0;
//		_cond = 0;
	}
}

Event::~Event() {


	if(_isAsync==false){
		pthread_mutex_destroy(&_mutex);
		pthread_cond_destroy(&_cond);
	}
	std::map<std::string, EventContext*>::iterator iter = _mapContext.begin();
	for (; iter != _mapContext.end(); iter++) {
		EventContext *context = iter->second;
		delete context;
	}
	_mapContext.clear();

}

void Event::addContext(const std::string &key,EventContext *cx)
{
	_mapContext.insert(make_pair(key,cx));
}

EventContext* Event::contextWithKey(const std::string &key)
{
	std::map<std::string,EventContext*>::iterator iter = _mapContext.find(key);
	if(iter == _mapContext.end()){
		return 0;
	}
	return iter->second;
}

void Event::addVcontext(const std::string &key,void *cx)
{
	TEventContext<void> *tec = new TEventContext<void>(cx,false);
	addContext(key,tec);
}
void* Event::vContextWithKey(const std::string &key)
{
	TEventContext<void> *tec = dynamic_cast<TEventContext<void>*>(contextWithKey(key));
	if(tec){
		void* v = tec->get();
		return v;
	}else{
		return 0;
	}

}

//void Event::initMutex()
//{
//
//}






}
